home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / AppsToGo / DTS.StyleChat / Window.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-22  |  31.2 KB  |  1,036 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** File:        Window.c
  5. ** Written by:    Eric Soldan
  6. **
  7. ** Copyright © 1990-1993 Apple Computer, Inc.
  8. ** All rights reserved.
  9. */
  10.  
  11. /* You may incorporate this sample code into your applications without
  12. ** restriction, though the sample code has been provided "AS IS" and the
  13. ** responsibility for its operation is 100% yours.  However, what you are
  14. ** not permitted to do is to redistribute the source as "DSC Sample Code"
  15. ** after having made changes. If you're going to re-distribute the source,
  16. ** we require that you make it clear in the source that the code was
  17. ** descended from Apple Sample Code, but that you've made changes. */
  18.  
  19. /* This file contains the code for the document procedure pointers for the main Wannabe
  20. ** document.  Wannabe currently only supports one type of documents, type 'DUMD',
  21. ** which stands for "DUMb Document". */
  22.  
  23. /* For more information on this file, please read the read.me file "=How to write your app". */ 
  24.  
  25.  
  26.  
  27. /*****************************************************************************/
  28.  
  29.  
  30.  
  31. #include "App.h"            /* Get the application includes/typedefs, etc.    */
  32. #include "App.defs.h"        /* Get various application definitions.            */
  33. #include "App.protos.h"        /* Get the prototypes for application.            */
  34.  
  35. #ifndef __ERRORS__
  36. #include <Errors.h>
  37. #endif
  38.  
  39. #ifndef __FONTS__
  40. #include <Fonts.h>
  41. #endif
  42.  
  43. #ifndef __RESOURCES__
  44. #include <Resources.h>
  45. #endif
  46.  
  47. #ifndef __TOOLUTILS__
  48. #include <ToolUtils.h>
  49. #endif
  50.  
  51. #ifndef __UTILITIES__
  52. #include "Utilities.h"
  53. #endif
  54.  
  55.  
  56.  
  57. /*****************************************************************************/
  58.  
  59.  
  60.  
  61. Boolean        gNoDefaultDocument = false;
  62.                     /* Set to true if app should boot with no default document. */
  63.                     /* This tells DTS.Lib..framework what you want. */
  64.  
  65. OSType        gAppWindowType = kDocFileType;    /* Main document type. */
  66. long        gAppWindowAttr = kwAppWindow;    /* Main window attributes. */
  67.  
  68. short        gMinVersion    = kMinVersion;    /* Minimum document version app can support. */
  69. short        gMaxVersion    = kMaxVersion;    /* Maximum document version app can support. */
  70.                                             /* More informing DTS.Lib..framework. */
  71.  
  72. extern Boolean        gStartingUp;
  73.  
  74. extern short        gPrintPage;                /* Non-zero means we are printing. */
  75.                                             /* DTS.Lib..framework global. */
  76.  
  77. extern RgnHandle    gCursorRgn;                /* We handle cursors here, so we need */
  78. extern CursPtr        gCursorPtr;                /* to know about these things. */
  79.                                             /* Above are DTS.Lib..framework globals. */
  80.  
  81. /* Currently Wannabe doesn't ever change the cursor, so we don't actually need
  82. ** these referenced here.  However, since Wannabe is supposed to be an application in
  83. ** progress, it is very likely that you will need to reference these as your project
  84. ** develops.  See DTS.StyleChat and DTS.Draw for examples of setting the cursor. */
  85.  
  86. /* Some cursors are pointer-based, and some cursors are resource-based.
  87. ** If a cursor is resource-based, it needs to be loaded and made to not move,
  88. ** and then gCursorPtr can be set to point to it.  This makes all cursors
  89. ** pointer-based.  Also, gCursorPtr is used by DTS.Lib..framework to
  90. ** determine if there is a current cursor.  If gCursorPtr is nil, then
  91. ** there is no current cursor, and the cursor has to be recalculated, no
  92. ** matter where the mouse is.  If gCursorPtr is not nil, then if the
  93. ** mouse position is within the cursor region gCursorRgn, the cursor is
  94. ** correct, and no recalculation is necessary.  If it is outside this region,
  95. ** then it is recalculated.  What does this all mean?  It means that if you
  96. ** want to guarantee that the cursor is recalculated next time DoWindowCursor()
  97. ** is called, set gCursorPtr to nil.
  98. **
  99. ** If you have a cursor resource, you need to:
  100. ** 1) Load the resource.
  101. ** 2) Make a fixed copy of it.
  102. ** 3) Set the cursor to it.
  103. ** 4) Set gCursorPtr to point to the fixed copy.
  104. **
  105. ** There is a function that does almost all of this, called DoSetResCursor().
  106. ** It does all but set gCursorPtr to it.  (It actually sets gCursorPtr to nil.)
  107. ** It does return a pointer to the permanent copy, so typically what you will
  108. ** want to do is the following:
  109. **     gCursorPtr = DoSetResCursor(theCursorID);
  110. **
  111. ** So why set gCursorPtr to nil as the default action?  This allows you to
  112. ** set a temporary cursor, which will be replaced when DoWindowCursor() is
  113. ** called next, or it allows you to set a cursor that maps to the cursor
  114. ** region gCursorRgn (by setting gCursorPtr to the return result). */
  115.  
  116. static Boolean    GoFast(TEHandle teHndl, EventRecord *event);
  117.  
  118.  
  119.  
  120. /*****************************************************************************/
  121. /*****************************************************************************/
  122.  
  123. #ifdef applec
  124. #pragma segment Window
  125. #endif
  126.  
  127. /*****************************************************************************/
  128. /*****************************************************************************/
  129.  
  130.  
  131.  
  132. /* •• You don't call this.  DTS.Lib..framework does for appropriate document type(s). •• */
  133.  
  134. /* Calculate application specific frame area (Called by DoCalcFrameRgn).
  135. ** You are passed an empty region.  You are supposed to add any custom frame
  136. ** parts that this document uses.  Typically there are no frame portions, as
  137. ** they are accounted for in other ways.  The scrollbars and grow icon will
  138. ** automatically be contributed to the calculation of the frame region.
  139. ** If you use sidebars, these are also added in automatically.  This is only
  140. ** used if the frame region is more complicated than can automatically be
  141. ** handled.  So, almost always, you will simply leave the region empty. */
  142.  
  143. void    CalcFrameRgn(FileRecHndl frHndl, WindowPtr window, RgnHandle rgn)
  144. {
  145. #ifndef __MWERKS__
  146. #pragma unused (frHndl, window, rgn)
  147. #endif
  148. }
  149.  
  150.  
  151.  
  152. /*****************************************************************************/
  153.  
  154.  
  155.  
  156. /* •• You don't call this.  DTS.Lib..framework does for appropriate document type(s). •• */
  157.  
  158. /* This is called (by DoContentClick()) when a mouse-down event occurs in the content of
  159. ** a window.  Other applications might want to call FindControl, TEClick, etc., to
  160. ** further process the click. */
  161.  
  162. void    ContentClick(WindowPtr window, EventRecord *event, Boolean firstClick)
  163. {
  164. #ifndef __MWERKS__
  165. #pragma unused (firstClick)
  166. #endif
  167.  
  168.     ControlHandle    ctl;
  169.     short            action, cnum;
  170.     FileRecHndl        frHndl;
  171.  
  172.     cnum = IsCtlEvent(window, event, &ctl, &action);
  173.         /* That was easy.  Scrolling was just handled.  Other stuff would be handled
  174.         ** by IsCtlEvent, if we had other stuff to do.  We don't have any other
  175.         ** controls in the content besides the document scrollbars. */
  176.  
  177.     frHndl = (FileRecHndl)GetWRefCon(window);
  178.     switch (cnum) {
  179.         case 1000:
  180.             SendMessage(frHndl, kStylMssg);
  181.             SendMessage(frHndl, kTextMssg);
  182.             break;
  183.         case 1010:
  184.             AllowAutoReconnect(frHndl);
  185.             SendConnect(frHndl, (char *)"\pDTS.Chat");
  186.             SetWindowDirty(window);
  187.             break;
  188.         case 1020:
  189.             if ((*frHndl)->connect.connected) {
  190.                 SendMessage(frHndl, kDisconnectMssg);
  191.                 (*frHndl)->connect.remotePath[0] = 0;
  192.             }
  193.             CNum2Ctl(window, 1020, &ctl);
  194.             (*ctl)->contrlVis = false;
  195.             BeginFrame(window);
  196.             CNum2Ctl(window, 1010, &ctl);
  197.             ShowStyledControl(ctl);
  198.             CNum2Ctl(window, 1000, &ctl);
  199.             (*ctl)->contrlVis = false;
  200.             CNum2Ctl(window, 1002, &ctl);
  201.             DoDraw1Control(ctl, false);
  202.             EndFrame(window);
  203.             break;
  204.     }
  205.  
  206.     return;
  207. }
  208.  
  209.  
  210.  
  211. /*****************************************************************************/
  212.  
  213.  
  214.  
  215. /* •• You don't call this.  DTS.Lib..framework does for appropriate document type(s). •• */
  216.  
  217. /* DoKeyDown() is first called by the application.  Then if the key isn't a menu
  218. ** key, DoKeyDown() calls this code.  Here are the rules for this function:
  219. **
  220. ** 1) If you handle the key, return(true).  This completes the key handling.
  221. ** 2) If you don't handle the key, you return false.  However, there are two
  222. **    situations for not handling the key:
  223. **      a) You want someone else to.
  224. **      b) You want nobody else to look at the key.
  225. **    This is what the boolean passThrough is for.  If you wish the next window
  226. **    to have a look at the key, set the boolean passThrough to true.  passThrough
  227. **    is already initialized to false, which is the common case, so you only have
  228. **    to worry about setting it true.
  229. **
  230. ** If you have a window that never processes keys and always passes them through,
  231. ** just set the contentKeyProc to nil.  This will indicate to the application
  232. ** framework that all keys should be passed through this window.  DTS.Draw has
  233. ** such a window.  Its palette window doesn't accept keys.  They are passed through
  234. ** to document windows. */
  235.  
  236. Boolean    ContentKey(WindowPtr window, EventRecord *event, Boolean *passThrough)
  237. {
  238. #ifndef __MWERKS__
  239. #pragma unused (passThrough)
  240. #endif
  241.  
  242.     short        cnum, action;
  243.     FileRecHndl        frHndl;
  244.  
  245.     cnum = IsCtlEvent(window, event, nil, &action);
  246.  
  247.     frHndl = (FileRecHndl)GetWRefCon(window);
  248.     switch (cnum) {
  249.         case 101:
  250.             if (action == 2) {
  251.                 SetWindowDirty(window);
  252.                 DoAdjustMenus();
  253.                 return(true);
  254.             }
  255.             break;
  256.         case 1000:
  257.             SendMessage(frHndl, kStylMssg);
  258.             SendMessage(frHndl, kTextMssg);
  259.             return(true);
  260.             break;
  261.     }
  262.  
  263.     return(false);
  264. }
  265.  
  266.  
  267.  
  268. /*****************************************************************************/
  269.  
  270.  
  271.  
  272. /* •• You don't call this.  DTS.Lib..framework does for appropriate document type(s). •• */
  273.  
  274. /* Draw application specific content (Called by DoDrawFrame).
  275. **
  276. ** If your application has any custom frame areas, or if it uses sidebars,
  277. ** this is the function that you would put the frame drawing code.  The
  278. ** document scrollbars and grow icon drawing is handled by DTS.framework.
  279. ** Just do the sidebar and custom areas here. */
  280.  
  281. void    DrawFrame(FileRecHndl frHndl, WindowPtr window, Boolean activate)
  282. {
  283.     MoveTo(0, (*frHndl)->fileState.topSidebar - 1);
  284.     LineTo((*frHndl)->fileState.leftSidebar - 1 - 16384, (*frHndl)->fileState.topSidebar - 1);
  285.     LineTo((*frHndl)->fileState.leftSidebar - 1 - 16384, 16383);
  286.  
  287.     BeginFrame(window);
  288.     DoDrawControls(window, activate);
  289.     EndFrame(window);
  290. }
  291.  
  292.  
  293.  
  294. /*****************************************************************************/
  295.  
  296.  
  297.  
  298. /* •• You don't call this.  DTS.Lib..framework does for appropriate document type(s). •• */
  299.  
  300. /* Frees up any application-specific memory in the document.  This is called by
  301. ** DoFreeDocument, which is called by DisposeDocument().  The application would
  302. ** call DisposeDocument(), not DoFreeDocument() or FreeDocument() directly.
  303. **
  304. ** The document may have a bunch of handles off the main handle of the document.
  305. ** This is where they are freed.  DisposeDocument calls this prior to releasing
  306. ** the ram for the main handle of the document, so release everything else
  307. ** here, or you will have a memory leak.
  308. **
  309. ** NOTE:  Calling DefaultFreeDocument() frees up all memory used by a
  310. ** hierarchical document (see TreeObj package). */
  311.  
  312. OSErr    FreeDocument(FileRecHndl frHndl)
  313. {
  314.     Handle            textHndl;
  315.     StScrpHandle    textStyl;
  316.  
  317.     textHndl = (*frHndl)->d.doc.textHndl;
  318.     if (textHndl) {
  319.         DisposeHandle(textHndl);
  320.         (*frHndl)->d.doc.textHndl = nil;
  321.     }
  322.     textStyl = (*frHndl)->d.doc.textStyl;
  323.     if (textStyl) {
  324.         DisposeHandle((Handle)textStyl);
  325.         (*frHndl)->d.doc.textStyl = nil;
  326.     }
  327.     DefaultFreeDocument(frHndl);
  328.  
  329.     return(noErr);
  330. }
  331.  
  332.  
  333.  
  334. /*****************************************************************************/
  335.  
  336.  
  337.  
  338. /* •• You don't call this.  DTS.Lib..framework does for appropriate document type(s). •• */
  339.  
  340. /* Any additional window disposal tasks can be handled here. */
  341.  
  342. OSErr    FreeWindow(FileRecHndl frHndl, WindowPtr window)
  343. {
  344. #ifndef __MWERKS__
  345. #pragma unused (window)
  346. #endif
  347.  
  348.     WindowPtr    ww;
  349.     FileRecHndl    ff;
  350.  
  351.     SendMessage(frHndl, kDisconnectMssg);
  352.         /* Let the remote user know the window is closing.  We must handle this,
  353.         ** as DTS.Lib..framework doesn't.   it is application-specific. */
  354.  
  355.     if ((*frHndl)->fileState.sfType == kDocFileType) {
  356.         for (ww = nil; (ww = GetNextWindow(ww, 0)) != nil;) {
  357.             ff = (FileRecHndl)GetWRefCon(ww);
  358.             if ((*ff)->fileState.sfType == kViewHierFileType) {
  359.                 if ((*frHndl)->d.doc.root == (*ff)->d.doc.root) {
  360.                     DisposeOneWindow(ww, kClose);
  361.                     ww = nil;
  362.                 }
  363.             }
  364.         }
  365.     }
  366.  
  367.     return(noErr);
  368.         /* We always return noErr here, even if SendMessage returned an error.  Why?
  369.         ** Because we don't want the closing of the window to halt just because the
  370.         ** network connection may have been lost, which is about the only error that
  371.         ** can occur.  If there's something bogus about the window, we most likely
  372.         ** want to get rid of it, with good riddance. */
  373. }
  374.  
  375.  
  376.  
  377. /*****************************************************************************/
  378.  
  379.  
  380.  
  381. /* •• You don't call this.  DTS.Lib..framework does for appropriate document type(s). •• */
  382.  
  383. /* Image the document into the current port.
  384. **
  385. ** The only thing tricky about this function is that it needs to key off of
  386. ** the global variable gPrintPage.  gPrintPage is the current page that is
  387. ** being printed.  If gPrintPage is 0, then you are drawing to the window.
  388. **
  389. ** For when printing:
  390. **
  391. ** If gPrintPage is non-0, that is the page to be printed.  If after imaging
  392. ** the page there are no more pages, you should set gPrintPage to 0.  This
  393. ** indicates to the print loop that the end of the document has been reached.
  394. ** Even if the user indicated in the job dialog to print more pages, setting
  395. ** gPrintPage to 0 states that the last page has been printed.  This is necessary
  396. ** because the print loop can't know when printing is done.  The imaging procedure
  397. ** is the logical one to state when everything has been imaged. */
  398.  
  399. OSErr    ImageDocument(FileRecHndl frHndl)
  400. {
  401.     WindowPtr        thePort;
  402.     Rect            rct, theInk;
  403.     TEHandle        te;
  404.     short            pageCol;
  405.     OSErr            err;
  406.     static short    taskOffset, taskNum;
  407.  
  408.     err = noErr;
  409.     GetPort(&thePort);
  410.  
  411.     if (!gPrintPage) {                                        /* If not printing... */
  412.         DoDrawControls(thePort, false);                        /* Draw the content controls. */
  413.     }
  414.     else {
  415.  
  416.         if (gPrintPage == 1)
  417.             taskOffset = taskNum = 0;
  418.  
  419.         theInk = thePort->portRect;
  420.         InsetRect(&theInk, 4, 4);        /* Just so no characters get clipped. */
  421.  
  422.         pageCol = 0;
  423.         for (rct = theInk; taskNum < 2;) {
  424.             te  = (taskNum) ? (*frHndl)->d.doc.outBox : (*frHndl)->d.doc.inBox;
  425.             err = CTEPrint(te, &taskOffset, &rct);
  426.             if (err) break;
  427.             if (taskOffset != -1) return(noErr);    /* Text went to bottom of page. */
  428.             taskOffset = 0;                            /* Done with this TextEdit record. */
  429.             ++taskNum;
  430.             rct.top    = rct.bottom + 20;
  431.             rct.bottom = theInk.bottom;
  432.             if (rct.top + 20 >= rct.bottom) return(noErr);
  433.                 /* No page left or not enough to bother with. */
  434.         }
  435.         gPrintPage = 0;
  436.     }
  437.  
  438.     return(err);
  439. }
  440.  
  441.  
  442.  
  443. /*****************************************************************************/
  444.  
  445.  
  446.  
  447. /* •• You don't call this.  DTS.Lib..framework does for appropriate document type(s). •• */
  448.  
  449. /* This function does the remaining window initialization.
  450. **
  451. ** There may be additional content initialization for the window.  At this point,
  452. ** you have a window, but it is currently invisible.  If you return noErr, then
  453. ** the window will be set to the state indicated for that window.  Why this function?
  454. ** You may wish to add controls to the content of the window.  You may have a
  455. ** TextEdit record in the content.  All of these sort of things can't be created
  456. ** until there is a window to contain them.  First a document is read in, and then
  457. ** if the document creation succeeds, a window is created for that document.
  458. ** At this point we have a document, and we are on our way to having a window.
  459. ** All that remains is any additional content initialization.  Do it, return
  460. ** noErr, and everybody's happy.  If something goes wrong here, return the error,
  461. ** and the incomplete window will be disposed of. */
  462.  
  463. OSErr    InitContent(FileRecHndl frHndl, WindowPtr window)
  464. {
  465.     OSErr            err;
  466.     WindowPtr        oldPort;
  467.     ControlHandle    ctl;
  468.     TEHandle        te;
  469.     Handle            text;
  470.     StScrpHandle    styl;
  471.  
  472.     err = AddControlSet(window, (*frHndl)->fileState.sfType, kwStandardVis, 0, 0, nil);
  473.     if (err) return(err);
  474.  
  475.     GetPort(&oldPort);
  476.     SetPort(window);
  477.  
  478.     CNum2Ctl(window, 100, &ctl);
  479.     (*frHndl)->d.doc.inBox = (TEHandle)GetControlReference(ctl);
  480.  
  481.     CNum2Ctl(window, 101, &ctl);
  482.     (*frHndl)->d.doc.outBox = te = (TEHandle)GetControlReference(ctl);
  483.     CTESetFastKeys(te, GoFast);
  484.  
  485.     text = (*frHndl)->d.doc.textHndl;
  486.     if (text) {
  487.         styl = (*frHndl)->d.doc.textStyl;
  488.         DisposeHandle(CTESwapText(te, text, styl, false));
  489.         (*frHndl)->d.doc.textHndl = nil;
  490.         if (styl) {
  491.             DisposeHandle((Handle)styl);
  492.             (*frHndl)->d.doc.textStyl = nil;
  493.         }
  494.     }
  495.  
  496.     ResizeContent(window, 0, 0);
  497.  
  498.     if (!(*frHndl)->connect.connected) {
  499.         if (!(*frHndl)->connect.remoteLoc.dataHandle) {
  500.             CNum2Ctl(window, 1010, &ctl);
  501.             (*ctl)->contrlVis = 0xFF;
  502.         }
  503.     }
  504.  
  505.     SetDocSize(frHndl, kwNoChange, 4096);
  506.  
  507.     SetPort(oldPort);
  508.     return(noErr);
  509. }
  510.  
  511. static Boolean    GoFast(TEHandle teHndl, EventRecord *event)
  512. {
  513. #ifndef __MWERKS__
  514. #pragma unused (teHndl)
  515. #endif
  516.  
  517.     if (event->modifiers & cmdKey) return(false);
  518.     return(true);
  519. }
  520.  
  521.  
  522.  
  523. /*****************************************************************************/
  524.  
  525.  
  526.  
  527. /* •• You don't call this.  DTS.Lib..framework does for appropriate document type(s). •• */
  528.  
  529. OSErr    ReadDocument(FileRecHndl frHndl)
  530. {
  531.     short            fileRefNum, oldRes;
  532.     OSErr            err;
  533.     char            hstate;
  534.     long            count;
  535.     Handle            textHndl;
  536.     StScrpHandle    styl;
  537.     ConnectRec        **ach;
  538.     AEAddressDesc    remoteLoc;
  539.     long            timeout;
  540.  
  541.     fileRefNum = (*frHndl)->fileState.refNum;
  542.  
  543.     err = SetFPos(fileRefNum, fsFromStart, 0);
  544.         /* Set the file position to the beginning of the file. */
  545.  
  546.     if (!err)
  547.         err = DoReadDocumentHeader(frHndl);
  548.             /* Read the document header.
  549.             ** Note that this doesn't guarantee that we will actually do something.
  550.             ** Text documents don't have a header.  The read and write header procs have
  551.             ** been set to nil for text documents, so calling DoReadDocumentHeader
  552.             ** will do nothing for text documents.  DTS.StyleChat documents do have a header,
  553.             ** and by calling DoReadDocumentHeader, the header will be read in. */
  554.  
  555.     if (err == resNotFound) err = noErr;
  556.  
  557.     if (!err) {        /* Read TextEdit text from file. */
  558.         textHndl = NewHandle(kMaxNumChars);
  559.         if (!(err = MemError())) {
  560.             count = kMaxNumChars;
  561.                 /* The size of the text isn't saved to disk.  This is the maximum
  562.                 ** that a TextEdit record can accept. */
  563.             hstate = LockHandleHigh(textHndl);
  564.             err    = FSRead(fileRefNum, &count, *textHndl);
  565.             HSetState(textHndl, hstate);
  566.  
  567.             if (err == eofErr)
  568.                 err = noErr;
  569.  
  570.             if (err)
  571.                 count = 0;
  572.  
  573.             SetHandleSize(textHndl, count);
  574.                 /* Set the handle to the actual size of the text on disk. */
  575.             (*frHndl)->d.doc.textHndl = textHndl;
  576.         }
  577.     }
  578.  
  579.     if (!err) {
  580.         err = UseDocResFile(frHndl, &oldRes, fsRdWrPerm);
  581.         if (!err) {
  582.             styl = (StScrpHandle)GetResource(kStylResType, kStylResID);
  583.             if (styl)
  584.                 DetachResource((Handle)styl);
  585.             (*frHndl)->d.doc.textStyl = styl;
  586.         }
  587.         UseResFile(oldRes);
  588.     }
  589.  
  590.     if (!err) {
  591.         err = UseDocResFile(frHndl, &oldRes, fsRdWrPerm);
  592.         if (!err) {
  593.             ach = (ConnectRec **)GetResource(kAutoConnectResType, kAutoConnectResID);
  594.             if (ach) {
  595.                 BlockMove(*ach, &(*frHndl)->connect, sizeof(ConnectRec));
  596.                 (*frHndl)->connect.connected            = false;
  597.                 (*frHndl)->connect.remoteLoc.dataHandle = nil;
  598.                 DetachResource((Handle)ach);
  599.                 DisposeHandle((Handle)ach);
  600.                 if ((*frHndl)->connect.remoteMachine[0]) {
  601.                     LaunchRemoteApp(frHndl);
  602.                     for (timeout = TickCount() + 600; timeout > TickCount();) {
  603.                         err = GetRemoteProcessTarget(frHndl, &remoteLoc, AEPortFilter);
  604.                         if (err) break;
  605.                         if (remoteLoc.dataHandle) {
  606.                             (*frHndl)->connect.remoteLoc = remoteLoc;
  607.                             SendConnect(frHndl, (char *)"\pDTS.Chat");
  608.                             break;
  609.                         }
  610.                     }
  611.                 }
  612.             }
  613.         }
  614.         UseResFile(oldRes);
  615.     }
  616.  
  617.     return(err);
  618. }
  619.  
  620.  
  621.  
  622. /*****************************************************************************/
  623.  
  624.  
  625.  
  626. /* •• You don't call this.  DTS.Lib..framework does for appropriate document type(s). •• */
  627.  
  628. /* Resize application specific content (Called by ResizeWindow).
  629. **
  630. ** This gets called when a user does a zoom or window resizing operation.
  631. ** It is possible that things in the content need to be resized in conjunction
  632. ** with the resizing of the window. */
  633.  
  634. void    ResizeContent(WindowPtr window, short oldh, short oldv)
  635. {
  636. #ifndef __MWERKS__
  637. #pragma unused (oldh, oldv)
  638. #endif
  639.  
  640.     FileRecHndl    frHndl;
  641.     WindowPtr    oldPort;
  642.     short        i;
  643.     Rect        crct, rct;
  644.     TEHandle    te;
  645.  
  646.     if (!window) return;
  647.  
  648.     frHndl  = (FileRecHndl)GetWRefCon(window);
  649.     oldPort = SetFilePort(frHndl);
  650.  
  651.     GetContentRect(window, &crct);
  652.     OffsetRect(&crct, -crct.left, -crct.top);
  653.     for (i = 0; i < 2; ++i) {
  654.         rct = crct;
  655.         --rct.top;
  656.         --rct.left;
  657.         rct.right  -= 14;
  658.  
  659.         rct.bottom = rct.top + (rct.bottom - rct.top + 1) / 2;
  660.         if (i) {
  661.             rct.top = rct.bottom - 1;
  662.             rct.bottom = crct.bottom + 1;
  663.         }
  664.  
  665.         te = (i) ? (*frHndl)->d.doc.outBox : (*frHndl)->d.doc.inBox;
  666.         CTEMove(te, rct.left, rct.top);
  667.         CTESize(te, rct.right - rct.left, rct.bottom - rct.top, true);
  668.     }
  669.  
  670.     SetPort(oldPort);
  671. }
  672.  
  673.  
  674.  
  675. /*****************************************************************************/
  676.  
  677.  
  678.  
  679. /* •• You don't call this.  DTS.Lib..framework does for appropriate document type(s). •• */
  680.  
  681. /* Scroll application specific frame (Called by DoScrollFrame).
  682. **
  683. ** Some applications may need to scroll the "frame" of the document along
  684. ** with the document contents.  This is common for applications with rulers,
  685. ** or other similar sidebar items. */
  686.  
  687. void    ScrollFrame(FileRecHndl frHndl, WindowPtr window, long dh, long dv)
  688. {
  689. #ifndef __MWERKS__
  690. #pragma unused (frHndl, window, dh, dv)
  691. #endif
  692. }
  693.  
  694.  
  695.  
  696. /*****************************************************************************/
  697.  
  698.  
  699.  
  700. /* •• You don't call this.  DTS.Lib..framework does for appropriate document type(s). •• */
  701.  
  702. /* Since the hierarchical document package isn't used by DTS.StyleChat,
  703. ** this function actually never gets called. */
  704.  
  705. void    UndoFixup(FileRecHndl frHndl, Point contOrg, Boolean afterUndo)
  706. {
  707. #ifndef __MWERKS__
  708. #pragma unused (frHndl, contOrg, afterUndo)
  709. #endif
  710.  
  711.     /* See DTS.Draw for an example of what you might do here. */
  712. }
  713.  
  714.  
  715.  
  716. /*****************************************************************************/
  717.  
  718.  
  719.  
  720. /* •• You don't call this.  DTS.Lib..framework does for appropriate document type(s). •• */
  721.  
  722. /* This function is where you adjust the cursor to reflect the location in the
  723. ** document or window.  You have the additional input of gCursorRgn to deal
  724. ** with.  The way that the cursor handling works is as follows:
  725. ** 1) The application calls DoWindowCursor().
  726. ** 2) DoWindowCursor() works its way through the windows/documents, front to back.
  727. **    It looks at the document's windowCursorProc and checks to see if the document
  728. **    has one.  If the document doesn't have one, then it assumes that that window
  729. **    always wants an arrow.  If the cursor is over that window, the cursor is set
  730. **    to an arrow, and we're done.  If the cursor isn't over the window, then the next
  731. **    window is tried.  If all documents don't have a windowCursorProc, then the cursor
  732. **    is set to an arrow (for the non-document area of the screen).
  733. ** 3) If a document has a windowCursorProc, then the proc is called.  The proc's
  734. **    job is as follows:
  735. **    a) If the cursor is over a position that is determined by the window, then
  736. **       the proc removes other areas from gCursorRgn.  Note that it should not
  737. **       simply set the area to what it "thinks" is the correct area.  This window
  738. **       may not be the front-most.  Other windows will have already been subtracted
  739. **       from gCursorRgn.  The resultant gCursorRgn is the correct cursor area,
  740. **       and should be passed to WaitNextEvent calls in the application (already the case
  741. **       in EventLoop.c).  Also, the cursor should be set to the correct cursor, of course.
  742. **       You should also return true, as the cursor has been determined.
  743. **    b) If the cursor is not over a position for this window, then you should
  744. **       return.  You will either pass back true or false.  If you don't wish
  745. **       windows behind this window to have a shot at cursor determination, then
  746. **       return true.  This states that the cursor is "determined".  It is, in the
  747. **       sense that no further determination will occur.  If you return false, then
  748. **       other windows get a shot at determining the cursor.
  749. **
  750. ** Setting the cursor to the correct cursor isn't as easy as you would expect.
  751. ** DTS.Lib..framework uses the global gCursorPtr as the reference to the cursor.  This is
  752. ** fine if the cursor is pointer-based, but if the cursor is resource-based, it is a bit
  753. ** more of a problem.  What you will need to do is to call DoSetResCursor() to make the
  754. ** resource cursor pointer-based.  DoSetResCursor() will set gCursorPtr to nil, and it
  755. ** also returns the pointer to the permanent copy of the cursor resource.  Just set gCursorPtr
  756. ** to the return result of DoSetResCursor(), and you will be set. */
  757.  
  758. Boolean    WindowCursor(FileRecHndl frHndl, WindowPtr window, Point globalPt)
  759. {
  760.     WindowPtr        oldPort;
  761.     RgnHandle        contRgn;
  762.     Rect            outBoxRct, teViewRct, contRct;
  763.     TEHandle        outBox, teHndl;
  764.     ControlHandle    viewCtl;
  765.     Point            contOrg;
  766.  
  767.     /* For the DTS.StyleChat sample, we display an i-beam cursor when over the outbox. */
  768.  
  769.     if (!window) {
  770.         SetCursor(gCursorPtr = &qd.arrow);
  771.         return(true);
  772.     }
  773.  
  774.     oldPort = SetFilePort(frHndl);
  775.     GetContentOrigin((window = (*frHndl)->fileState.window), &contOrg);
  776.     SetOrigin(contOrg.h, contOrg.v);        /* Scroll position of window. */
  777.  
  778.     viewCtl   = CTEViewFromTE(outBox = (*frHndl)->d.doc.outBox);
  779.     outBoxRct = (*viewCtl)->contrlRect;        /* Local coordinates of outbox. */
  780.     InsetRect(&outBoxRct, 2, 2);
  781.  
  782.     GetContentRect(window, &contRct);
  783.         /* This returns the content portion of the window in local coordinates,
  784.         ** less document scrollbar and sidebar areas. */
  785.  
  786.     SectRect(&outBoxRct, &contRct, &outBoxRct);
  787.         /* Part of the outbox still visible after scrolling. */
  788.  
  789.     LocalToGlobalRect(&outBoxRct);        /* The outbox rect, in global coordinates. */
  790.  
  791.     SetPort(oldPort);                    /* Put the port back. */
  792.  
  793.     CopyRgn(((WindowPeek)window)->contRgn, (contRgn = NewRgn()));
  794.  
  795.     if (CTETargetInfo(&teHndl, &teViewRct) == window) {
  796.         /* If target TextEdit control belongs to this window... */
  797.  
  798.         if ((teHndl == outBox) && (PtInRect(globalPt, &outBoxRct))) {
  799.             /* If target TextEdit control is the outbox, and the cursor
  800.             ** is over the visible part of the outbox... */
  801.  
  802.             gCursorPtr = DoSetResCursor(ibeamCursor);
  803.             RectRgn(contRgn, &outBoxRct);
  804.             SectRgn(gCursorRgn, contRgn, gCursorRgn);
  805.             DisposeRgn(contRgn);
  806.             return(true);
  807.         }
  808.     }
  809.  
  810.     return(false);
  811. }
  812.  
  813.  
  814.  
  815. /*****************************************************************************/
  816.  
  817.  
  818.  
  819. /* •• You don't call this.  DTS.Lib..framework does for appropriate document type(s). •• */
  820.  
  821. /* After the DTS.Lib framework disposes of a window, it calls here.  This is
  822. ** to give the application a chance to do any additional tasks related to
  823. ** a window closing.  DTS.StyleChat doesn't have anything else extra to do. */
  824.  
  825. void    WindowGoneFixup(WindowPtr window)
  826. {
  827. #ifndef __MWERKS__
  828. #pragma unused (window)
  829. #endif
  830. }
  831.  
  832.  
  833.  
  834. /*****************************************************************************/
  835.  
  836.  
  837.  
  838. /* •• You don't call this.  DTS.Lib..framework does for appropriate document type(s). •• */
  839.  
  840. /* The reverse function of ReadDocument. */
  841.  
  842. OSErr    WriteDocument(FileRecHndl frHndl)
  843. {
  844.     short            fileRefNum, oldRes;
  845.     OSErr            err;
  846.     char            hstate;
  847.     long            count, fpos;
  848.     TEHandle        te;
  849.     Handle            textHndl, oldr, ach;
  850.     StScrpHandle    styl;
  851.     ConnectRec        ac;
  852.  
  853.     fileRefNum = (*frHndl)->fileState.refNum;
  854.  
  855.     err = SetFPos(fileRefNum, fsFromStart, 0);
  856.         /* Set the file position to the beginning of the file. */
  857.  
  858.     if (!err)
  859.         err = DoWriteDocumentHeader(frHndl);
  860.             /* Write the document header.
  861.             ** Note that this doesn't guarantee that we will actually do something.
  862.             ** Text documents don't have a header.  The read and write header procs have
  863.             ** been set to nil for text documents, so calling DoWriteDocumentHeader
  864.             ** will do nothing for text documents.  DTS.StyleChat documents do have a header,
  865.             ** and by calling DoWriteDocumentHeader, the header will be written out. */
  866.  
  867.     if (!err) {        /* Write out-box TextEdit control text to file. */
  868.         te       = (*frHndl)->d.doc.outBox;
  869.         textHndl = (*te)->hText;
  870.         count    = (*te)->teLength;
  871.         hstate   = LockHandleHigh(textHndl);
  872.         err      = FSWrite(fileRefNum, &count, *textHndl);
  873.         HSetState(textHndl, hstate);
  874.     }
  875.  
  876.     if (!err) {
  877.         err = GetFPos(fileRefNum, &fpos);
  878.         if (!err)
  879.             err = SetEOF(fileRefNum, fpos);
  880.     }            /* The document may be shorter than last time it was written to disk.
  881.                 ** Handle this case by ending the file based on the new length. */
  882.  
  883.     if (!err) {
  884.         styl = CTEGetFullStylScrap((*frHndl)->d.doc.outBox);
  885.         if (styl) {
  886.             err = UseDocResFile(frHndl, &oldRes, fsRdWrPerm);
  887.             if (!err) {
  888.                 oldr = GetResource(kStylResType, kStylResID);
  889.                 if (oldr) RemoveResource(oldr);
  890.                 AddResource((Handle)styl, kStylResType, kStylResID, nil);
  891.                 WriteResource((Handle)styl);
  892.                 DetachResource((Handle)styl);
  893.                 DisposeHandle((Handle)styl);
  894.             }
  895.             UseResFile(oldRes);
  896.         }
  897.     }
  898.  
  899.     if (!err) {
  900.         err = UseDocResFile(frHndl, &oldRes, fsRdWrPerm);
  901.         if (!err) {
  902.             oldr = GetResource(kAutoConnectResType, kAutoConnectResID);
  903.             if (oldr) RemoveResource(oldr);
  904.             ac = (*frHndl)->connect;
  905.             if (ac.remotePath[0]) {
  906.                 ach = NewHandle(sizeof(ConnectRec));
  907.                 if (ach) {
  908.                     BlockMove(&ac, *ach, sizeof(ConnectRec));
  909.                     AddResource(ach, kAutoConnectResType, kAutoConnectResID, nil);
  910.                     WriteResource(ach);
  911.                     DetachResource(ach);
  912.                     DisposeHandle(ach);
  913.                 }
  914.             }
  915.         }
  916.         UseResFile(oldRes);
  917.     }
  918.  
  919.     return(err);
  920. }
  921.  
  922.  
  923.  
  924. /*****************************************************************************/
  925.  
  926.  
  927.  
  928. /* •• You don't call this.  DTS.Lib..framework does at open-application time. •• */
  929.  
  930. OSErr    DoOpenApplication(void)
  931. {
  932.     MenuHandle    menu;
  933.  
  934.     gStartingUp = true;
  935.  
  936.     menu = GetMenuHandle(mFonts);
  937.     if (menu)
  938.         AppendResMenu(menu, 'FONT');
  939.  
  940.     return(noErr);
  941. }
  942.  
  943.  
  944.  
  945. /*****************************************************************************/
  946. /*****************************************************************************/
  947. /*****************************************************************************/
  948.  
  949.  
  950.  
  951. /* •• You don't call this.  DTS.Lib..framework does for appropriate document type(s). •• */
  952.  
  953. Boolean    AdjustMenuItems(WindowPtr window, short menuID)
  954. {
  955.     Boolean        redrawMenuBar;
  956.     MenuHandle    menu;
  957.  
  958.     redrawMenuBar = false;
  959.  
  960.     switch (menuID) {
  961.         case mFile:
  962.             redrawMenuBar = DoAdjustFileMenu(window);
  963.             break;
  964.         case mEdit:
  965.             redrawMenuBar = DoAdjustEditMenu(window);
  966.             break;
  967.         case mFonts:
  968.             redrawMenuBar = DoAdjustFontsMenu(window);
  969.             break;
  970.         default:
  971.             menu = GetMenuHandle(menuID);
  972.             if (menu)
  973.                 (*menu)->enableFlags |= 0xFFFFFFFEL;
  974.             break;
  975.     }
  976.  
  977.     return(redrawMenuBar);
  978. }
  979.  
  980.  
  981.  
  982. /*****************************************************************************/
  983.  
  984.  
  985.  
  986. /* •• You don't call this.  DTS.Lib..framework does for appropriate document type(s). •• */
  987.  
  988. Boolean    DoMenuItem(WindowPtr window, short menuID, short menuItem)
  989. {
  990. #ifndef __MWERKS__
  991. #pragma unused (window)
  992. #endif
  993.  
  994.     return(DoMenuCommand(menuID, menuItem));
  995. }
  996.  
  997.  
  998.  
  999. /*****************************************************************************/
  1000. /*****************************************************************************/
  1001. /*****************************************************************************/
  1002.  
  1003.  
  1004.  
  1005. OSErr    DuplicateDocument(FileRecHndl oldFrHndl, FileRecHndl *newFrHndl)
  1006. {
  1007.     OSErr        err;
  1008.     TEHandle    te;
  1009.     Handle        oldHndl, newHndl;
  1010.     long        size;
  1011.  
  1012.     err = NewDocument(newFrHndl, (*oldFrHndl)->fileState.sfType, true);
  1013.         /* Create a document and root object to copy the file data into. */
  1014.  
  1015.     if (err) return(err);
  1016.  
  1017.     te      = (*oldFrHndl)->d.doc.outBox;
  1018.     oldHndl = (*te)->hText;
  1019.     size    = (*te)->teLength;
  1020.     newHndl = NewHandle(size);
  1021.     if (newHndl) {
  1022.         (**newFrHndl)->d.doc.textHndl = newHndl;
  1023.         BlockMove(*oldHndl, *newHndl, size);
  1024.     }
  1025.     else {
  1026.         err = MemError();
  1027.         DisposeDocument(*newFrHndl);
  1028.         *newFrHndl = nil;
  1029.     }
  1030.  
  1031.     return(err);
  1032. }
  1033.  
  1034.  
  1035.  
  1036.